/**********************************************************************/
/*
Author: Karan Makkar
Created: Jun 2024
Updated: Aug 2025, by Youssef Assarssah
Description: Master program to run and output SUS Outcomes regression, 
restricting the sample to applicants who were their HH's survey takers.
Output: Table A10     
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_sus_outcomes_pwin_master_surveytaker.txt", text replace

/*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Regression programs
include "$KP_do/3 Functions/impact_regression_programs"

* Table formatting and flipping programs
include "$KP_do/3 Functions/impact_table_programs"

* Set filepaths for dataset
global mar21 "${KP_deid_susenas}/Clean/sus_mar21_deid_clean_merged.dta"
global sep21 "${KP_deid_susenas}/Clean/sus_sep21_deid_clean_merged.dta"
global mar22 "${KP_deid_susenas}/Clean/sus_mar22_deid_clean_merged.dta"

/*----------------------------------------------------*/
* Section: Outcome variable lists
/*----------------------------------------------------*/

* Table 1: Main Table Vars
global susonly totcons ///
            employed ///
            self_emp_bus_owner ///
            hours_worked ///

local consvar totcons
local pc_suff _tot


/*----------------------------------------------------*/
* Section: Regression Specifications
/*----------------------------------------------------*/

* SE CLUSTERING (OLS): Individual level
global vce vce(cluster anon_id4)

* INSTRUMENT (IV): win in batch
global instrument_iv win_in_batch

* SE CLUSTERING (IV): Individual level
global cluster cluster(anon_id4)

* FIXED EFFECTS: Strata and inputs into PMO-assigned weight interacted by batch
global absorb absorb(c.stratum_win_prob#batch gender#batch urban#batch ed_cat#batch)

* FIXED EFFECTS (STACKED REGRESSION): Strata, SUSENAS wave and inputs into PMO-assigned weight interacted by batch and SUSENAS wave
global absorb_stacked_sus absorb(c.stratum_win_prob#batch c.stratum_win_prob#sus_round gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round)

* SAMPLE RESTRICTIONS: restrict to person-batch observations before date surveyed

global precomma_5 inrange(batch, 2, 5)
global precomma_11 inrange(batch, 2, 11)
global precomma_18 inrange(batch, 2, 18)
global precomma_22 inrange(batch, 2, 22)
global precomma_stack_22 ((sus_round == 5 & inrange(batch, 2, 5)) | (sus_round == 6 & inrange(batch, 2, 11)) | (sus_round == 7 & inrange(batch, 2, 18)) | (sus_round == 8 & inrange(batch, 2, 22)))

/*----------------------------------------------------------------*/
* Section: Data Setup
/*----------------------------------------------------------------*/

* Load data and append
use "${mar21}", clear
append using "${sep21}"
append using "${mar22}"

*Education cat
recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

*Age cat
cap drop age_cat
recode age (0/30 = 1) (31/50 = 2) (51/100 = 3), gen(age_cat) 

mdesc age_cat gender ed_cat multi_apply_hh urban strata

* X VAR: Generate placeholder
gen x = 0
la var x "Treated"

* Hours worked name regularize
replace hours_worked = hrs_worked_all if inlist(sus_round, 6, 8)

*Win before survey var
gen win_before_survey = .
replace win_before_survey  = ever_win_11 if sus_round == 6 & inrange(batch, 2, 11)
replace win_before_survey  = ever_win_18 if sus_round == 7 & inrange(batch, 2, 18)
replace win_before_survey  = ever_win_22 if sus_round == 8 & inrange(batch, 2, 22)

* HH level win before survey
gen hhid = RENUM if inlist(sus_round, 5, 6)
replace hhid = urut if sus_round ==7
replace hhid = URUT if sus_round ==8
bysort hhid batch sus_round: egen win_before_surveyhh = max(win_before_survey)

* Report Selected SUS
gen report_selected =. if inrange(sus_round, 5, 8)
replace report_selected = hh_pk_win if inlist(sus_round, 6, 7)
replace report_selected = get_pk if inlist(sus_round, 5, 8)

* HH Level Report Selected
bysort hhid batch sus_round: egen report_selectedhh = max(report_selected)

* Win at first attempt
bysort anon_id4 sus_round (batch): gen winfirst = win_in_batch ==1 & _n ==1

* Education Dummy
gen above_hs = 1 if inrange(education, 4, 8)
replace above_hs = 0 if inrange(education, 1, 3)

* Total consumption
gen totcons = tot_adj_cons*hh_size_sus

* Drop non-randomized batches
drop if inlist(batch, 1, 15)

* Keep only survey takers
keep if surveytaker ==1

/*----------------------------------------------------------------*/
* Section: IV Regressions: Main Table
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_su iva_11su iva_18 iva_22
global column_num 4

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $susonly

* loop through each variable of table
foreach y of varlist $vars {

    * Run IV regression (actual win)
    replace x = win_before_survey

    *Sus Regs

    *Stacked
    iv_reg `y'_iva_su `y' x "$instrument_iv" "" "$precomma_stack_22" "$absorb_stacked_sus" "$cluster" // (batch 2-22)
    * Mar 21
    iv_reg `y'_iva_11su `y' x "$instrument_iv" "" "$precomma_11 & sus_round ==6" "$absorb" "$cluster" // (batch 2-11)
    * Sep 21
    iv_reg `y'_iva_18 `y' x "$instrument_iv" "" "$precomma_18 & sus_round ==7" "$absorb" "$cluster" // (batch 2-18)
    * Mar 22
    iv_reg `y'_iva_22 `y' x "$instrument_iv" "" "$precomma_22 & sus_round ==8" "$absorb" "$cluster" // (batch 2-22)

   * Add models to list
    global model_list $model_list `y'_iva_su `y'_iva_11su `y'_iva_18 `y'_iva_22 
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA10.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table "$columns" "$KP_output/tables/impacts/tex/TableA10" "`consvar'"